package net.stratusprogramming.school.Game.Framework.System;

import org.newdawn.slick.util.Log;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Logger {
	
	private static SimpleDateFormat date;
	private static SimpleDateFormat dateFromStart;
	private static long start;
	private static boolean tsFromStart = true; 

	public static void startLogger() {
		start = System.currentTimeMillis();
		NumberFormat fmt = NumberFormat.getInstance();
		fmt.setMinimumIntegerDigits(2);
	
		date = new SimpleDateFormat("kk:MM:ss:SSSS a"); 
		date.setTimeZone(Calendar.getInstance().getTimeZone());		
		dateFromStart = new SimpleDateFormat("HH:MM:ss:SSSS"); 

		dateFromStart.setTimeZone(TimeZone.getTimeZone("GMT-0"));
		Log.setVerbose(false);
	}
	
	public static void logln(String message) {
		log(null, message, true);
	}
	
	public static void logln(String id, String message) {
		log(id, message, true);
	}
	
	public static void log(String message) {
		log(null, message, true);
	}
		
	public static void log(String message, boolean newline) {
		log(null, message, newline);
	}
	
	public static void log(String id, String message, boolean newline) {
		StringBuilder sb = new StringBuilder();
		sb.append("[Stratus: ");
		
		// time stamp
		if(tsFromStart) 
			sb.append(dateFromStart.format(new Date(System.currentTimeMillis()-start-60000)));
		else
			sb.append(date.format(new Date()));
		sb.append("] ");
		
		// append id?
		if(id != null && id.length() != 0) {
			sb.append("[");
			sb.append(id);
			sb.append("] ");
		}		
		
		sb.append(message);
		if(newline) sb.append("\r\n");
		System.out.print(sb.toString());
	}
	
	public static <T> void logVar(String name, T value) {
		logVar(null, name, value);
	}
	
	public static <T> void logVar(String id, String name, T value) {
		StringBuilder sb = new StringBuilder();
		sb.append("[Stratus: ");
		
		// time stamp
		if(tsFromStart) 
			sb.append(dateFromStart.format(new Date(System.currentTimeMillis()-start-60000)));
		else
			sb.append(date.format(new Date()));
		sb.append("] ");
		
		// append id?
		if(id != null && id.length() != 0) {
			sb.append("[");
			sb.append(id);
			sb.append("] ");
		}		
		
		sb.append(name);
		sb.append(": ");
		sb.append(value);
		sb.append("\r\n");
		System.out.print(sb.toString());
	}
	
	public static boolean toggleTimestamp() {
		tsFromStart = !tsFromStart;
		return tsFromStart;
	}
}
